package com.peaker.advertisingpro.utils;

import com.google.gson.reflect.TypeToken;
import com.peaker.advertisingpro.bean.NullData;
import com.peaker.advertisingpro.bean.ResultBean;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESCBCUtil {
    private static final String ENCODING = "GBK";

    private static final String KEY_ALGORITHM = "AES";
    /**
     * 加解密算法/工作模式/填充方式
     */
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
    /**
     * 填充向量
     */
    private static final String FILL_VECTOR = "1234560405060708";



    /**
     * 加密字符串
     *
     * @param content  字符串
     * @return
     * @throws
     */
    public static String encrypt(String content, String password) {

        if (content.isEmpty() || password.isEmpty()) {
//            log.error("AES decryption params is null");
            return null;
        }

        byte[] raw = hex2byte(password);
        SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(FILL_VECTOR.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] anslBytes = content.getBytes(ENCODING);
            byte[] encrypted = cipher.doFinal(anslBytes);
            return byte2hex(encrypted).toUpperCase();
        } catch (Exception e) {
//            log.error("AES encryption operation has exception,content:{},password:{}", content, password, e);
        }
        return null;
    }

    /**
     * 解密
     *
     * @param content  解密前的字符串
     * @return
     * @throws
     * @author cdduqiang
     * @date 2014年4月3日
     */
    public static String decrypt(String content,  String password) {
        if (content.isEmpty() || password.isEmpty()) {
//            log.error("AES decryption params is null");
            return null;
        }


        try {
            byte[] raw = hex2byte(password);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(FILL_VECTOR.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = hex2byte(content);
            byte[] original = cipher.doFinal(encrypted1);
            return new String(original, ENCODING);
        } catch (Exception e) {
            e.printStackTrace();
//            log.error("AES decryption operation has exception,content:{},password:{}", content, password, e);
        }
        return null;
    }

    public static byte[] hex2byte(String strhex) {
        if (strhex == null) {
            return null;
        }
        int l = strhex.length();
        if (l % 2 == 1) {
            return null;
        }
        byte[] b = new byte[l / 2];
        for (int i = 0; i != l / 2; i++) {
            b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2), 16);
        }
        return b;
    }

    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
    }

    public static void main(String[] args) throws Exception {
        String str = "Hello world";
        //必须为16位
        //生成加密密钥
        String key2 = "666823353237344A40662A7236355223";
        System.out.println(key2);
//        String encryptStr = encrypt(str, key2);
//        System.out.println(encryptStr);
        System.out.println(decrypt("E4145B27012128413760B65058A59D7D9D7FCA5098C66C77A68E2C8A53312A953FE822ADE4ACA6886310BF1F9C3414AD9FEB447CAD1B301E0B8E5652372EE568BF2013595A6C8A38BC9E7CECB5A1C34970E52A79D46365BD6460C272EFA5E4F3E01F15AE3213AB35EE309502473ABEB51C7A05C1D76F7682BF5659CF72A9BF54EA7A89E2A42CB5C302D9E91724A14885A92489EBF5DA8E6034B75EECADDEC12FC11CBC3ACA43236F8C13227312E3384CC9D7E2FC229540D62B2E69F6C4974B10947EE92D1B359193917024A1A063DBBD", "666823353237344A40662A7236355223"));

        String encrypt = encrypt("{\"data\":\"E4145B27012128413760B65058A59D7D9D7FCA5098C66C77A68E2C8A53312A953FE822ADE4ACA6886310BF1F9C3414AD9FEB447CAD1B301E0B8E5652372EE568BF2013595A6C8A38BC9E7CECB5A1C34970E52A79D46365BD6460C272EFA5E4F3E01F15AE3213AB35EE309502473ABEB51C7A05C1D76F7682BF5659CF72A9BF54EA7A89E2A42CB5C302D9E91724A14885A92489EBF5DA8E6034B75EECADDEC12FC11CBC3ACA43236F8C13227312E3384CC9D7E2FC229540D62B2E69F6C4974B10947EE92D1B359193917024A1A063DBBD\",\"encrypt\":\"1\",\"type\":\"1001\"}", "666823353237344A40662A7236355223");
        System.out.println("加密: "+encrypt);

        String dec=decrypt(encrypt,"666823353237344A40662A7236355223");
        System.out.println("解密: "+dec);



    }
}